interface IDataObject {
    [key: string]: any
}


interface IDefaultDataObject {
    [key: string]: {
        //默认值
        default: any,
        //保留小数位
        toFixedNum?: number
        //数据量级
        levelConvert?: number
    }
}

const defaultFormatterObjects: IDefaultDataObject = {
    tax: {
        default: 0,
        toFixedNum: 2
    },
    taxAcc: {
        default: 0,
        toFixedNum: 2
    },
    taxAccPeriod: {
        default: 0,
        toFixedNum: 2
    },
    taxAccGrowthRate: {
        default: "-",
        toFixedNum: 2
    },
    districtRevenue: {
        default: 0,
        toFixedNum: 2
    },
    districtRevenueAcc: {
        default: 0,
        toFixedNum: 2
    },
    districtRevenueAccPeriod: {
        default: 0,
        toFixedNum: 2
    },
    districtRevenueAccGrowthRate: {
        default: "-",
        toFixedNum: 2
    },
    //首页主体情况
    taxEntNum: {
        default: 0,
    },
    aboveScaleEntNum: {
        default: 0,
    },
    taxOtherPlaceEntNum: {
        default: 0,
    },
    aboveScaleOtherPlaceEntNum: {
        default: 0,
    },
    //企业资源主体情况
    newregEntNum: {
        default: 0,
    },
    newregTaxEntNum: {
        default: 0,
    },
}

//todo 使用defaultFormatterObjects思路优化格式化函数
/**
 * 默认格式化参数
 * 将需要修改默认值的key加入defaultFormatterObject
 */
const defaultFormatterObject: IDataObject = {
    tax: 0,
    taxAcc: 0,
    taxAccPeriod: 0,
    taxAccGrowthRate: "-",
    districtRevenue: 0,
    districtRevenueAcc: 0,
    districtRevenueAccPeriod: 0,
    districtRevenueAccGrowthRate: "-",
    //首页主体情况
    taxEntNum: 0,
    aboveScaleEntNum: 0,
    taxOtherPlaceEntNum: 0,
    aboveScaleOtherPlaceEntNum: 0,
    //企业资源主体情况
    newregEntNum: 0,
    newregTaxEntNum: 0,
}
/**
 * 数值类属性保留两位小数参数
 * 将需要保留两位小数的key加入defaultToFixedArray
 */
const defaultToFixedArray: Array<string> = ["tax", "taxAcc", "taxAccPeriod", "taxAccGrowthRate",
    "districtRevenue", "districtRevenueAcc", "districtRevenueAccPeriod", "districtRevenueAccGrowthRate"]
/**
 * 数值类量级转换参数
 * 将需要转换量级的key加入defaultLevelArray
 */
const defaultLevelArray: Array<string> = ["taxAcc", "districtRevenueAcc"]

const isEmpty = (value: Object): Boolean => {
    if (value === '' || value == null || value == undefined) {
        return true;
    }
    return false;
}

/*const arrMap = (arr: Array<IDataObject>, fn: Function) => {
    return arr.map((obj: IDataObject) => {
        return fn(obj);
    });
}*/

export const ecoDataFormatter = {
    /**
     * 转换arr中obj对象属性的数据量级
     * @param arr 处理对象
     * @param level 数据量级，默认10000
     */
    arrayLevelConversion: (arr: Array<IDataObject>, level: number = 10000): Array<IDataObject> => {
        return arr.map((obj: IDataObject) => {
            return ecoDataFormatter.objectLevelConversion(obj, level);
        });
    },
    /**
     * 转换obj对象属性的数据量级
     * @param obj 处理对象
     * @param level 数据量级，默认10000
     */
    objectLevelConversion: (obj: IDataObject, level: number = 10000): IDataObject => {
        let data = obj;
        const levelConversionObj: IDataObject = {};
        for (const item in defaultLevelArray) {
            if (obj.hasOwnProperty(item)) {
                levelConversionObj[item] = obj[item] / level;
            }
        }
        Object.assign(data, levelConversionObj)
        return data;
    },
    /**
     * 将传入的Array<IDataObject>按照配置的默认值进行格式化
     * @param arr 需要格式化的Array<IDataObject>
     * @return 按照配置格式化后的arr
     */
    arrayFormatter: (arr: Array<IDataObject>): Array<IDataObject> => {
        return arr.map((obj: IDataObject) => {
            return ecoDataFormatter.objectFormatter(obj);
        });
    },
    /**
     * 将传入的IDataObject按照配置的默认值进行格式化
     * @param obj 需要格式化的IDataObject
     * @return 按照配置格式化后的obj
     */
    objectFormatter: (obj: IDataObject): IDataObject => {
        let data = obj;
        const formattedObj: IDataObject = {};
        for (const key in defaultFormatterObject) {
            if (obj.hasOwnProperty(key) && (isEmpty(obj[key]))) {
                formattedObj[key] = defaultFormatterObject[key];
            } else if (obj.hasOwnProperty(key)) {
                formattedObj[key] = obj[key];
            }
        }
        Object.assign(data, formattedObj)
        return data;
    },
    /**
     * 将数组对象中的每个属性按照配置保留两位小数
     * @param arr 数组对象
     * @return 按照配置将属性保留小数后的数组对象
     */
    arrayToFixed: (arr: Array<IDataObject>): Array<IDataObject> => {
        return arr.map((obj: IDataObject) => {
            return ecoDataFormatter.objectToFixed(obj);
        });
    },
    /**
     * 将对象中的数值属性按照配置保留两位小数
     * @param obj 处理对象
     * @param fractionDigits 小数位，默认2
     * @return 按照配置将属性保留小数后的数组对象
     */
    objectToFixed: (obj: IDataObject, fractionDigits: number = 2): IDataObject => {
        let data = obj;
        const toFixedObj: IDataObject = {};
        defaultToFixedArray.map((item: string) => {
            if (obj.hasOwnProperty(item)) {
                toFixedObj[item] = obj[item].toFixed(fractionDigits);
            }
        })
        Object.assign(data, toFixedObj)
        return data;
    },
    /**
     * 格式化默认值加保留小数位
     * @param arr 处理数组对象
     * @param fractionDigits 保留小数位
     */
    arrayFormatterAndToFixed: (arr: Array<IDataObject>, fractionDigits: number = 2): Array<IDataObject> => {
        return arr.map((obj: IDataObject) => {
            return ecoDataFormatter.objectFormatterAndToFixed(obj, fractionDigits);
        });
    },
    /**
     * 格式化默认值加保留小数位
     * @param obj 处理对象
     * @param fractionDigits 保留小数位
     */
    objectFormatterAndToFixed: (obj: IDataObject, fractionDigits: number = 2): IDataObject => {
        let formatter = ecoDataFormatter.objectFormatter(obj);
        let toFixed = ecoDataFormatter.objectToFixed(formatter, fractionDigits);
        return toFixed;
    },
}